#include "returnwindow.h"

ReturnWindow::ReturnWindow(const QString &bookId, Database *db, QWidget *parent)
    : QDialog(parent)
    , bookId(bookId)
    , db(db)
{
    setupUI();
    createConnections();
}

ReturnWindow::~ReturnWindow()
{
}

void ReturnWindow::setupUI()
{
    setWindowTitle("归还图书");
    setFixedSize(300, 200);

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    mainLayout->setSpacing(20);
    mainLayout->setContentsMargins(20, 20, 20, 20);

    // 图书ID显示
    QLabel *bookIdLabel = new QLabel(QString("图书ID: %1").arg(bookId), this);
    bookIdLabel->setStyleSheet("font-size: 14px;");
    mainLayout->addWidget(bookIdLabel);

    // 用户ID输入
    QLabel *userIdLabel = new QLabel("用户ID:", this);
    userIdEdit = new QLineEdit(this);
    userIdEdit->setPlaceholderText("请输入用户ID");
    userIdEdit->setStyleSheet("QLineEdit { padding: 8px; border: 1px solid #BDBDBD; border-radius: 4px; }");
    mainLayout->addWidget(userIdLabel);
    mainLayout->addWidget(userIdEdit);

    // 按钮
    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->setSpacing(20);

    returnButton = new QPushButton("归还", this);
    cancelButton = new QPushButton("取消", this);

    returnButton->setStyleSheet("QPushButton { background-color: #9C27B0; color: white; padding: 8px 16px; "
                              "border: none; border-radius: 4px; }"
                              "QPushButton:hover { background-color: #7B1FA2; }"
                              "QPushButton:pressed { background-color: #4A148C; }");

    cancelButton->setStyleSheet("QPushButton { background-color: #F44336; color: white; padding: 8px 16px; "
                              "border: none; border-radius: 4px; }"
                              "QPushButton:hover { background-color: #D32F2F; }"
                              "QPushButton:pressed { background-color: #B71C1C; }");

    buttonLayout->addWidget(returnButton);
    buttonLayout->addWidget(cancelButton);
    mainLayout->addLayout(buttonLayout);

    // 设置窗口样式
    setStyleSheet("QDialog { background-color: white; }");
}

void ReturnWindow::createConnections()
{
    connect(returnButton, &QPushButton::clicked, this, &ReturnWindow::onReturnClicked);
    connect(cancelButton, &QPushButton::clicked, this, &ReturnWindow::onCancelClicked);
}

void ReturnWindow::onReturnClicked()
{
    QString userId = userIdEdit->text().trimmed();

    if (userId.isEmpty()) {
        QMessageBox::warning(this, "警告", "请输入用户ID！");
        return;
    }

    if (db->returnBook(bookId, userId)) {
        QMessageBox::information(this, "成功", "图书归还成功！");
        accept();
    } else {
        QMessageBox::warning(this, "错误", "图书归还失败！");
    }
}

void ReturnWindow::onCancelClicked()
{
    reject();
} 